package com.aaa.service.proxy;

import com.aaa.service.Service1;
import com.aaa.service.impl.Service1Impl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxyFactory {
    public Object createProxyObject(Object obj) {
        Object object = Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("开启事务");
                Object o1 = methodProxy.invokeSuper(o, objects);
                System.out.println("关闭事务");
                return o1;
            }
        });
        return object;
    }
}

class TestCglib {
    public static void main(String[] args) {
        Service1 service1 = new Service1Impl();
        Service1 proxyObject = (Service1) new CglibProxyFactory().createProxyObject(service1);
        proxyObject.myService();
    }

}